package red.mlz.module.module.tag.service;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import red.mlz.module.module.tag.entity.MaterialsTagsRelation;
import red.mlz.module.module.tag.entity.Tag;
import red.mlz.module.module.tag.mapper.MaterialsTagsRelationMapper;
import red.mlz.module.module.tag.mapper.TagMapper;
import red.mlz.module.utils.BaseUtils;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class TagService {
    @Resource
    private TagMapper mapper;

    @Resource
    private MaterialsTagsRelationMapper materialsTagsRelationMapper;


    public Tag getById(BigInteger id) {
        return mapper.getById(id);
    }

    public Tag extractById(BigInteger id) {
        return mapper.extractById(id);
    }

    public void insert(Tag Tag) {
        mapper.insert(Tag);
    }

    public int update(Tag Tag) {
        return mapper.update(Tag);
    }

    public void delete(BigInteger id) {
        mapper.delete(id, BaseUtils.currentSeconds());
    }


    @Transactional
    public List<Tag> insertBatch(String tags) {
        // 1.解析并去重标签
        List<String> tagNames = Arrays.stream(tags.split(","))
                .map(String::trim)
                .filter(name -> !name.isEmpty())
                .distinct()
                .collect(Collectors.toList());

        List<Tag> oldTagList = mapper.listByNames(tagNames);
        List<String> newTagNames = tagNames;
        List<Tag> tagList = new ArrayList<>();
        // 2.过滤旧标签
        if (!BaseUtils.isEmpty(oldTagList)) {
            Set<String> existingTagNames
                    = oldTagList.stream().map(Tag::getName).collect(Collectors.toSet());
            newTagNames = tagNames.stream()
                    .filter(tag -> !existingTagNames.contains(tag))
                    .collect(Collectors.toList());
            tagList.addAll(oldTagList);
        }
        // 3.插入
        if (!BaseUtils.isEmpty(newTagNames)) {
            List<Tag> newTagList = newTagNames.stream().map(tagName -> {
                Tag tag = new Tag();
                tag.setName(tagName);
                tag.setCreateTime(BaseUtils.currentSeconds());
                tag.setUpdateTime(BaseUtils.currentSeconds());
                tag.setIsDeleted(0);
                return tag;
            }).collect(Collectors.toList());
            mapper.insertBatch(newTagList);
            tagList.addAll(newTagList);
        }
        return tagList;
    }

    public List<Tag> getByMaterialId(BigInteger id) {
        List<MaterialsTagsRelation> materialsTagsRelationList = materialsTagsRelationMapper.listByMaterialId(id);
        String tagIds = materialsTagsRelationList
                .stream()
                .map(MaterialsTagsRelation::getTagId)
                .map(String::valueOf)
                .collect(Collectors.joining(","));
        return mapper.listByIds(tagIds);
    }



}
